package com.shiku.imserver;

import com.shiku.imserver.common.ImConfig;
import com.shiku.imserver.common.utils.PropertiesUtil;
import com.shiku.imserver.common.utils.StringUtils;
import com.shiku.imserver.common.ws.IWsMsgHandler;
import com.shiku.imserver.common.ws.WsServerConfig;
import com.shiku.imserver.hander.ImServerHandler;
import com.shiku.imserver.listener.ImServerListener;
import com.shiku.imserver.listener.ShikuGroupListener;
import com.shiku.imserver.service.IMBeanUtils;
import java.io.IOException;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.server.intf.ServerAioListener;


public class IMServerStarter {
    private static Logger log = LoggerFactory.getLogger(IMServerStarter.class);
    private ImServerGroupContext serverGroupContext;
    private ImServerHandler serverHandler;
    private ServerAioListener serverListener;
    private ShikuTioServer tioServer;
    private IWsMsgHandler wsMsgHandler;

    public IMServerStarter() {
        this(null, null);
    }

    public static void main(String[] args) throws Exception {
        IMServerStarter imServerStarter = new IMServerStarter();
        String propertyFile = null;
        try {
            if (args.length > 0) {
                propertyFile = args[0];
            }
            ImConfig config = initImConfig(propertyFile);
            IMBeanUtils.initialize(config, imServerStarter.getServerGroupContext());
            imServerStarter.start("", config.getBindPort().intValue());
        } catch (Exception e) {
            log.error(" 启动服务器 异常  {}", e.getMessage());
        }
    }

    public IWsMsgHandler getWsMsgHandler() {
        return this.wsMsgHandler;
    }

    public ImServerGroupContext getServerGroupContext() {
        return this.serverGroupContext;
    }

    public IMServerStarter(WsServerConfig wsServerConfig, IWsMsgHandler wsMsgHandler2) {
        this.wsMsgHandler = null;
        this.serverHandler = null;
        this.serverListener = null;
        this.serverGroupContext = null;
        this.tioServer = null;
        try {
            this.wsMsgHandler = wsMsgHandler2;
            this.serverHandler = new ImServerHandler();
            this.serverListener = new ImServerListener();
            this.serverGroupContext = new ImServerGroupContext(this.serverHandler, this.serverListener);
            this.serverGroupContext.setHeartbeatTimeout(180000);
            this.serverGroupContext.setGroupListener(new ShikuGroupListener());
            this.tioServer = new ShikuTioServer(this.serverGroupContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void start(String ip, int port) throws IOException {
        this.tioServer.start(ip, port);
    }

    public static ImConfig initImConfig(String url) throws Exception {
        String propertyFile = "classpath:imserver.properties";
        if (!StringUtils.isEmpty(url)) {
            propertyFile = url;
        }
        Properties properties = PropertiesUtil.loadPropertyFile(propertyFile);
        if (properties == null) {
            log.error("加载配置文件失败  =====》 ");
            throw new Exception("加载配置文件失败  =====》 ");
        }
        ImConfig config = new ImConfig(properties);
        config.initImConfig();
        return config;
    }
}
